package cn.eonshine.cuba.inf.core.web.config;

import io.undertow.Undertow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.eonshine.cuba.inf.core.properties.HttpProperties;


/**
 * Spring Boot支持Undertow服务器，支持http、https(ssl、tls)双协议，支持双端口 <br>
 * 参考：https://blog.csdn.net/typa01_kk/article/details/77101391 <br>
 * UndertowEmbeddedServletContainerFactory 在springboot2.0以后的版本，更名为UndertowServletWebServerFactory <br>
 * 详细说明参考：<br>
 * https://stackoverflow.com/questions/45581389/where-is-undertowembeddedservletcontainerfactory-in-spring-boot-2-0-0
 * Undertow 详细配置参考： <br>
 * https://blog.csdn.net/rickiyeat/article/details/78906366 <br>
 * https://www.cnblogs.com/softidea/p/6068128.html <br>
 * 
 * 此类的初始化由@ConditionalOnProperty进行判断<br>
 * 作为第二个端口协议，判断：<br>
 * 如果存在http.port=8080的配置，那么久初始化这个端口，继续执行此类初始化，并出示bean <br>
 * 
 * @author Alan
 *
 */
@Slf4j
@Configuration
@ConditionalOnProperty(prefix="http", name="port")
@EnableConfigurationProperties(HttpProperties.class)
public class UndertowConfig {
	
	@Autowired
    private HttpProperties properties;

    @Bean
    public UndertowServletWebServerFactory embeddedServletContainerFactory() {
    	UndertowServletWebServerFactory undertow = new UndertowServletWebServerFactory();
        undertow.addBuilderCustomizers((Undertow.Builder builder) -> {
            builder.addHttpListener(properties.getPort(), "0.0.0.0");
        });
        log.info("\n*** Undertow http setting successful." + properties.getPort());
        return undertow;
    }
}
